package cn.ffcs.uec.staff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staff.action.bean.UecNationdutyEditBean;
import cn.ffcs.uec.staff.model.UecNationduty;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecNationdutyEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecNationdutyEditBean bean = new UecNationdutyEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 添加职业资格管理
	 */
	private UecNationduty uecNationduty;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecNationdutyEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		// 最高istop
		List<NodeVo> nodeVoIstopList = new ArrayList<NodeVo>();
		String[][] istop = { { "Y", "是" }, { "N", "否" } };
		for (int i = 0; i < istop.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(istop[i][j]);
				nodeVo.setName(istop[i][j + 1]);
			}
			nodeVoIstopList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getIstop(), nodeVoIstopList);
		// 岗位职业资格匹配度
		ListboxUtils.rendererForEdit(bean.getGlbdef5(), nodeVoIstopList);

		// 职业资格等级worklevel
		List<NodeVo> nodeVoLevelList = new ArrayList<NodeVo>();
		String[][] worklevel = { { "1001A1100000000006DJ", "一级" },
				{ "1001A1100000000006DK", "二级" },
				{ "1001A1100000000006DL", "三级" },
				{ "1001A1100000000006DM", "四级" },
				{ "1001A1100000000006DN", "五级" },
				{ "1001A1100000000006DO", "不分等级" } };
		for (int i = 0; i < worklevel.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(worklevel[i][j]);
				nodeVo.setName(worklevel[i][j + 1]);
			}
			nodeVoLevelList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getWorklevel(), nodeVoLevelList);

		//职业资格名称workname
		List<NodeVo> nodeVoWorknameList = new ArrayList<NodeVo>();
		String[][] workname = { 
				{ "1001A1100000001VKN8B", "店长" },
				{ "1001A1100000001VKNAS", "政企客户级服务经理" },
				{ "1001A1100000001VKNAU", "网络维护-承载网络技术支撑（数据网）" },
				{ "1001A1100000001VKNB5", "网络维护-互联网安全技术支撑" }
				};
		for (int i = 0; i < workname.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(workname[i][j]);
				nodeVo.setName(workname[i][j + 1]);
			}
			nodeVoWorknameList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getWorkname(), nodeVoWorknameList);
		
		//职业资格类别occupation
		List<NodeVo> nodeVoOccupationList = new ArrayList<NodeVo>();
		String[][] occupation = { 
				{ "1001A1100000000006DP", "社会通用认证" },
				{ "1001A1100000000006DQ", "行业认证" },
				{ "1001A1100000000006DR", "企业内部认证-集团认证" },
				{ "1001A1100000000006DS", "企业内部认证-省公司认证" },
				{ "1001A1100000000006DT", "无职业资格名称" }
				};
		for (int i = 0; i < occupation.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(occupation[i][j]);
				nodeVo.setName(occupation[i][j + 1]);
			}
			nodeVoOccupationList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getOccupation(), nodeVoOccupationList);
		
	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecNationdutyEditWindow().setTitle("新增职业资格管理");
			bean.getUecNationdutyEditDiv().setHeight("30%");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecNationdutyEditWindow().setTitle("查看职业资格管理");
			uecNationduty = (UecNationduty) arg.get("uecNationduty");
			if (null != uecNationduty) {
				PubUtil.fillBeanFromPo(uecNationduty, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecNationdutyEditWindow(),
						true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecNationdutyEditWindow().setTitle("编辑职业资格管理");
			bean.getUecNationdutyEditDiv().setHeight("30%");
			uecNationduty = (UecNationduty) arg.get("uecNationduty");
			if (null != uecNationduty) {
				PubUtil.fillBeanFromPo(uecNationduty, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecNationduty = UecNationduty.newInstance();
			PubUtil.fillPoFromBean(bean, uecNationduty);
			dateTransToString();
			uecNationduty.setStaffId(uecStaffInfo.getStaffId());
			uecNationduty.addOnly();
			Events.postEvent("onOK", this.self, uecNationduty);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecNationduty);
			dateTransToString();
			uecNationduty.updateOnly();
			Events.postEvent("onOK", this.self, uecNationduty);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecNationdutyEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getOccupation().getSelectedItem().getValue())) {
			return "请填写职业资格类别";
		} 
		
		return null;
	}

	/**
	 * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		// 获证日期
		if (!StrUtil.isNullOrEmpty(bean.getWorkdate().getValue())) {
			uecNationduty.setWorkdate(formatter.format(bean.getWorkdate()
					.getValue()));
		}
		// 证书有效期
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef4().getValue())) {
			uecNationduty.setGlbdef4(formatter.format(bean.getGlbdef4()
					.getValue()));
		}
	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 获证日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != uecNationduty.getWorkdate()) {
			bean.getWorkdate().setValue(sdf.parse(uecNationduty.getWorkdate()));
		}
		// 证书有效期
		if (null != uecNationduty.getGlbdef4()) {
			bean.getGlbdef4().setValue(sdf.parse(uecNationduty.getGlbdef4()));
		}
	}

}
